<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/10/26
 * Time: 18:06
 */

namespace app\MAPP\admin\ctl;

use function core\alert;
use core\lib\BaseControlerDB;
use function core\p;
use Extentions\template\Template;
use app\MAPP\admin\mode\login\func as login;

class fackcmsc extends BaseControlerDB
{

    private $mode;
    function __USERFUNC()
    {
        $this->mode = new login();
    }

    function index()
    {
        if (!isset($_SESSION[$_COOKIE["uname"]])){
            header("Location: /run/fackcms/login");
            return;}
        $tpl = Template::Newinstance();
        $tpl->assign("menber",$_SESSION[$_COOKIE["uname"]]);
        $tpl->display("cms/index.html");
    }

    function login()
    {
        $tpl = Template::Newinstance();
        $tpl->assign("title","LOGIN");
        $tpl->display("cms/login.html");
        if (!$this->msg->isPost()){
            return;
        }
        $uname = $this->msg->Postaparm("uname");
        $passwd = $this->msg->Postaparm("passwd");

        if ($this->mode->login($uname,$passwd)){
            setcookie("uname",$uname,
                time()+60*60,"/");
            $_SESSION[$uname] = $uname;
            header("Location: /run/fackcms/index");
        }else{
            session_unset();
            alert("invalid user");
        }
    }

    function reg()
    {
        $tpl = Template::Newinstance();
        $tpl->assign("title","REGISTER");
        $tpl->assign("action","./reg");
        $tpl->assign("method","post");
        $tpl->display("RL.html");

        if (!$this->msg->isPost()){
            return;
        }

        $uname = $this->msg->Postaparm("uname");
        $passwd = $this->msg->Postaparm("passwd");

        $temp0 = $this->mode->usercheck($uname);
        if (count($temp0)>0){
            throw new \Exception("aready registed");
        }
        try{
            $temp = $this->mode->reg($uname,$passwd);
            p("reg ok");
        }catch (\ErrorException $e){
            p("reg faillllll");
        }
    }

    function logout(){
        unset($_SESSION[$_COOKIE["uname"]]);
        setcookie("uname","");
        header("Location: /run/fackcms/login");
    }

    function news(){
        $tpl = Template::Newinstance();
        $tpl->assign("data",$this->mode->booklist());
        $tpl->display("cms/news_list.html");
    }

    function addNews(){
        $tpl = Template::Newinstance();
        $tpl->assign("ueditor",
            file_get_contents("E:\app\server\phpstudy_pro\myframwork\blog\app\\views\ueditor.html"));
        $tpl->display("cms/news_add.html");
        if (!$this->msg->isPost()){
            return;
        }

        try{
            $temp = $this->mode->addnews(
                $this->msg->Postaparm("title"),
                $this->msg->Postaparm("author"),
                $this->msg->Postaparm("content")
            );
            alert("插入成功");
        }catch (\ErrorException $e){
            alert("插入失败");
            p($e->getMessage());
        }
    }

    function delnews($id){
        try{
            $temp = $this->mode->delnews($id);
            alert("ok");
        }catch (\ErrorException $e){
            alert("fail");
        }

    }

    function delmany(){
        if (!$this->msg->isPost()){
            return;
        }
        $ids = $this->msg->Postparms();
        try{
            if ($ids == null){
                alert("fail");
                return;
            }
            $ids = array_values($ids["idarr"]);
            $this->mode->delmany($ids);
            alert("ok");
        }catch (\ErrorException $e){
            alert("fail");
        }

    }


}